package com.zx.servicelearn.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.widget.Toast;

/**
 * 利用messenger进行跨进程通信
 */
public class MyService5 extends Service {
    String TAG = "MyService5";

    public MyService5() {
    }

    public static final int MSG_SAY_HELLO = 1;

    Messenger messenger = new Messenger(new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_SAY_HELLO:
                    Log.e(TAG, "handleMessage");//
                    Toast.makeText(MyService5.this, "啦啦啦！", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    });

    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind");//调用startService或bindService时候会执行一次，第二次调用不会执行
        return messenger.getBinder();
    }

    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");//调用startService或bindService时候会执行一次，第二次调用不会执行
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");//调用startService时候会执行
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind");//
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");//调用stopService会执行
        super.onDestroy();
    }
}
